package com.you.atlas.web.place.module.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.Group;
import com.alibaba.citrus.service.uribroker.uri.URIBroker;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.turbine.dataresolver.Params;
import com.you.atlas.biz.place.service.NoteService;
import com.you.atlas.web.place.vo.NotePhotoVO;
import com.you.common.webx.AuthContext;
import com.you.place.dal.dao.dataobject.NoteDO;
import com.you.place.dal.dao.dataobject.NoteItemDO;

public class NoteAction {

    @Autowired
    private NoteService noteService;

    public void doAdd(@Param("placeId") Long placeId, @FormGroup("postNote") Group group,
                      @Params NotePhotoVO notePhotoVO, Context context, Navigator navigator) {
        NoteDO noteDO = new NoteDO();
        group.setProperties(noteDO);
        noteDO.setPlaceId(placeId);
        noteDO.setMemberId(AuthContext.getContext().getMemberId());

        if (noteService.addNote(noteDO, notePhotoVO.getNotePhotoList())) {

            navigator.redirectTo("placeModule").withTarget("noteView.vm").withParameter("id",
                                                                                        noteDO.getId().toString());
        }
    }

    public void doAddNoteItem(@FormGroup("postNote") NoteItemDO noteItemDO, @Params NotePhotoVO notePhotoVO,
                              Context context, Navigator navigator) {
        noteItemDO.setNoteId(notePhotoVO.getNoteId());
        noteItemDO.setMemberId(AuthContext.getContext().getMemberId());
        if (noteService.addNoteItem(noteItemDO, notePhotoVO.getNotePhotoList())) {

            URIBroker uriBroker = navigator.redirectTo("placeModule").withTarget("noteView.vm").uri();
            uriBroker.addQueryData("id", noteItemDO.getNoteId()).setReference(noteItemDO.getId().toString());
        }
    }

    public void doModify(@FormGroup("postNote") Group group, @Params NotePhotoVO notePhotoVO, Context context,
                         Navigator navigator) {
        String title = group.getField("title").getValue().toString();
        String content = group.getField("content").getValue().toString();
        NoteItemDO noteItemDO = new NoteItemDO();
        Long noteId = notePhotoVO.getNoteId();
        Long noteItemId = notePhotoVO.getNoteItemId();
        noteItemDO.setId(noteItemId);
        noteItemDO.setNoteId(noteId);
        noteItemDO.setContent(content);
        if (noteService.modifyNote(title, noteItemDO, notePhotoVO.getNotePhotoList())) {
            URIBroker uriBroker = navigator.redirectTo("placeModule").withTarget("noteView.vm").uri();
            uriBroker.addQueryData("id", noteId).setReference(noteItemId.toString());
        }

    }

}
